package com.lrkj.arcface.service.impl;


import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FaceFeature;
import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.FaceSimilar;
import com.arcsoft.face.toolkit.ImageInfo;
import com.lrkj.arcface.controller.ArcFaceController;
import com.lrkj.arcface.service.AceFaceService;
import com.lrkj.facefeature.mapper.FaceFeatureMapper;
import com.lrkj.util.Base64ToFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.arcsoft.face.toolkit.ImageFactory.getRGBData;

/**
 * 人脸识别ace service 业务层处理
 *
 * @author wyd
 * @date 2022-07-13
 */
@Service
public class AceFaceServiceImpl implements AceFaceService {
    public static int errorCode;
    //public static FaceEngine faceEngine= ArcFaceController.faceEngine;

    @Autowired
    private FaceFeatureMapper faceFeatureMapper;

    @Override
    public String faceDisplayImg(String base64img) {
        FaceEngine faceEngine=ArcFaceController.faceEngine;
        String dataPrix = ""; //base64格式前头
        String data = "";//实体部分数据
        if(base64img==null||"".equals(base64img)){
            return "上传失败，上传图片数据为空";
        }else {
            String [] d = base64img.split("base64,");//将字符串分成数组
            if(d != null && d.length == 2){
                dataPrix = d[0];
                data = d[1];
            }else {
                return "上传失败，数据不合法";
            }
        }
        //图片存本地
        String time=String.valueOf(System.currentTimeMillis());
        Base64ToFile.base64ToFile("F:\\01wyd\\02facedis\\img",data,time+".png");
        String path="F:\\01wyd\\02facedis\\img\\"+time+".png";
        //比对
        //人脸检测
        ImageInfo imageInfo = getRGBData(new File(path));
        List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
        errorCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
        System.out.println(faceInfoList);
        //特征提取
        FaceFeature faceFeature = new FaceFeature();
        errorCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList.get(0), faceFeature);
        System.out.println("特征值大小：" + faceFeature.getFeatureData().length);
        //人脸检测2
        ImageInfo imageInfo2 = getRGBData(new File("F:\\01wyd\\02facedis\\img\\base.png"));
        List<FaceInfo> faceInfoList2 = new ArrayList<FaceInfo>();
        errorCode = faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(),imageInfo2.getImageFormat(), faceInfoList2);
        System.out.println(faceInfoList2);
        //特征提取2
        FaceFeature faceFeature2 = new FaceFeature();
        errorCode = faceEngine.extractFaceFeature(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), imageInfo2.getImageFormat(), faceInfoList2.get(0), faceFeature2);
        System.out.println("特征值大小：" + faceFeature2.getFeatureData().length);
        //特征比对
        FaceFeature targetFaceFeature = new FaceFeature();
        targetFaceFeature.setFeatureData(faceFeature.getFeatureData());
        FaceFeature sourceFaceFeature = new FaceFeature();
        sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData());
        FaceSimilar faceSimilar = new FaceSimilar();
        errorCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
        System.out.println("arcface相似度：" + faceSimilar.getScore());

        //信息存数据库
        com.lrkj.facefeature.domain.FaceFeature  feature=new com.lrkj.facefeature.domain.FaceFeature();
        feature.setFace_feature_data(faceFeature.getFeatureData());
        feature.setFace_img_path(path);
        feature.setEngine_type("1");
        faceFeatureMapper.insertFaceFeature(feature);
        return "相似度：" + faceSimilar.getScore();
    }
}
